package com.smz.service.impl;


import com.smz.dao.RoleDAO;
import com.smz.entity.PageEntity;
import com.smz.entity.Role;
import com.smz.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {


    @Autowired
    private RoleDAO roleDAO;



    @Override
    public List<Role> selectAllRoleByName(String username) {
        return roleDAO.selectAllRoleByName(username);
    }

    @Override
    public int selectRoleCount() {
        return roleDAO.selectRoleCount();
    }

    @Override
    public PageEntity<Role> findRolesPage(Integer current){
        int count = roleDAO.selectRoleCount();
        int pageCount = count % PageEntity.PAGE_SIZE == 0 ? count / PageEntity.PAGE_SIZE : count / PageEntity.PAGE_SIZE + 1;
        List<Role> data = roleDAO.selectRoleByPage((current-1)* PageEntity.PAGE_SIZE,current*PageEntity.PAGE_SIZE);

        //data数据要放第一个要不然无法识别参数类型
        return new PageEntity<>(data,current,count,pageCount);
    }

    @Override
    public void insertRole(Role role) {
        roleDAO.insertRole(role);

    }

    @Override
    public void updateRole(Role role) {
        roleDAO.updateRole(role);
    }

    @Override
    public void deleteRole(Integer id) {
        roleDAO.deleteRole(id);
    }

    @Override
    public List<Role> findAllRole() {
        return roleDAO.selectAllRole();
    }

    @Override
    public List<Role> findRoleById(Integer id) {
        return roleDAO.selectRoleById(id);
    }
}
